<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 零钱兑换 -->
    <script>
      function coinChange(coins, amount) {
        function dp(n) {
          if (n === 0) return 0
          if (n < 0) return -1
          let res = Infinity // 求最小值，初始化为正无穷
          for (let i = 0; i < coins.length; i++) {
            let subProblem = dp(n - coins[i])
            // 子问题无效，直接跳过
            if (subProblem === -1) continue
            res = Math.min(res, subProblem + 1)
          }
          return res === Infinity ? -1 : res
        }
        return dp(amount)
      }
      console.log(coinChange([1, 2, 5], 11))
    </script>
  </body>
</html>
